﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;

namespace Gosocket.SDK
{
    public class AppManager: WebManager
    {
        public AppManager(Guid ApplicationId, string ApplicationSecret)
        {
            _ApplicationId = ApplicationId;
            _Secret = ApplicationSecret;
            
            CreateWebClient();
        }

        public AppManager(String urlservice, Guid ApplicationId, string ApplicationSecret)
        {
            _default_url_service = urlservice;
            
            _ApplicationId = ApplicationId;
            _Secret = ApplicationSecret;
            
            CreateWebClient();
        }

        /// <summary>
        /// Get Xml File
        /// </summary>
        /// <param name="DocumentId">Document Id</param>
        /// <returns>Document byte[]</returns>
        public byte[] GetXml(string CountryId, Guid DocumentId)
        {
            string requestUri = string.Format("api/App/GetXml/?CountryId={0}&DocumentId={1}", CountryId, DocumentId);
            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            byte[] result = resp.Content.ReadAsAsync<byte[]>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// Get Distribution Rule
        /// </summary>
        /// <param name="AccountId">Account Id</param>
        /// <returns>IEnumerable<Domain.DistributionRule></returns>
        public Domain.Result GetDistributionRules(string CountryId, string ReceiverCode)
        {
            string requestUri = string.Format("api/App/GetDistributionRules/?CountryId={0}&ReceiverCode={1}", CountryId, ReceiverCode);
            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();           

            Domain.Result result = resp.Content.ReadAsAsync<Domain.Result>().Result;

            resp.Dispose();            

            return result;
        }
    }
}
